package com.tupu.service.impl.dispatch;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tupu.entity.dispatch.DadsCzpczx;
import com.tupu.entity.dispatch.DispatchOperTicketPerformMsg;
import com.tupu.mapper.dispatch.DadsCzpczxMapper;
import com.tupu.mapper.dispatch.DispatchOperTicketPerformMsgMapper;
import com.tupu.service.admin.PerformMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class PerformMsgServiceImpl extends ServiceImpl<DispatchOperTicketPerformMsgMapper, DispatchOperTicketPerformMsg> implements PerformMsgService {

    @Autowired(required = false)
    private DispatchOperTicketPerformMsgMapper dispatchOperTicketPerformMsgMapper;

    @Autowired
    private DadsCzpczxMapper dadsCzpczxMapper;

    @Override
    @DS("dfdz")
    public void addMsg(DispatchOperTicketPerformMsg msg) {
        DadsCzpczx dadsCzpczx = new DadsCzpczx();
        dadsCzpczx.setInstId(msg.getMsgId());
        dadsCzpczx.setCznr(msg.getOperContent());
/*        dadsCzpczx.setXlsj(msg.getPublishTime());
        dadsCzpczx.setXlr(msg.getPublishPeople());
        dadsCzpczx.setZxsj(msg.getReceiveTime());
        dadsCzpczx.setZxr(msg.getReceiver());
        dadsCzpczx.setJssj(msg.getFinishTime());
        dadsCzpczx.setLogicIndex(msg.getNodeNum());*/
        dadsCzpczx.setLsdx(msg.getDetailId());
        dadsCzpczxMapper.insert(dadsCzpczx);
    }

    @Override
    @DS("dfdz")
    public void delMsg(String msg_id) {
        dadsCzpczxMapper.deleteById(msg_id);
    }

    @Override
    @DS("dfdz")
    public DispatchOperTicketPerformMsg findById(String msg_id) {
        DadsCzpczx dadsCzpczx = dadsCzpczxMapper.selectById(msg_id);
        if (ObjectUtil.isEmpty(dadsCzpczx)) {
            return null;
        }
        DispatchOperTicketPerformMsg performMsg = new DispatchOperTicketPerformMsg();
        performMsg.setDetailId(dadsCzpczx.getInstId());
        performMsg.setMsgId(dadsCzpczx.getInstId());
        performMsg.setOperContent(dadsCzpczx.getCznr());
/*        performMsg.setPublishTime(dadsCzpczx.getXlsj());
        performMsg.setPublishPeople(dadsCzpczx.getXlr());
        performMsg.setReceiveTime(dadsCzpczx.getZxsj());
        performMsg.setReceiver(dadsCzpczx.getZxr());
        performMsg.setPerformTime(dadsCzpczx.getZxsj());
        performMsg.setFinishTime(dadsCzpczx.getJssj());
        performMsg.setNodeNum(dadsCzpczx.getLogicIndex());*/
        return performMsg;
    }

    @Override
    @DS("dfdz")
    public Integer moveUp(String msg_id) {
        DadsCzpczx dadsCzpczx = dadsCzpczxMapper.selectById(msg_id);
/*        Integer num = dadsCzpczx.getLogicIndex();
        String lsdx = dadsCzpczx.getLsdx();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("LSDX", lsdx);
        queryWrapper.eq("LOGIC_INDEX", num - 1);
        List<DadsCzpczx> downCzp = dadsCzpczxMapper.selectList(queryWrapper);
        if (ObjectUtil.isEmpty(downCzp)) {
            return 0;
        }
        dadsCzpczx.setLogicIndex(downCzp.get(0).getLogicIndex());
        dadsCzpczxMapper.updateById(dadsCzpczx);
        downCzp.get(0).setLogicIndex(num);
        dadsCzpczxMapper.updateById(downCzp.get(0));*/
        return 1;
    }

    @Override
    @DS("dfdz")
    public Integer moveDown(String msg_id) {
        DadsCzpczx dadsCzpczx = dadsCzpczxMapper.selectById(msg_id);
/*        Integer num = dadsCzpczx.getLogicIndex();
        String lsdx = dadsCzpczx.getLsdx();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("LSDX", lsdx);
        queryWrapper.eq("LOGIC_INDEX", num + 1);
        List<DadsCzpczx> upCzp = dadsCzpczxMapper.selectList(queryWrapper);
        if (ObjectUtil.isEmpty(upCzp)) {
            return 0;
        }
        dadsCzpczx.setLogicIndex(upCzp.get(0).getLogicIndex());
        dadsCzpczxMapper.updateById(dadsCzpczx);
        upCzp.get(0).setLogicIndex(num);
        dadsCzpczxMapper.updateById(upCzp.get(0));*/
        return 1;
    }
}
